using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using APJUtil = APJ.Util;

namespace MTRC.ODMS.Web.SOM.Popup
{
    /// <summary>
    /// Refer to "namespace MTRC.ODMS.Web.LPM.LostFound.GetPic"
    /// </summary>
    public partial class GetPic : System.Web.UI.Page //BasePage_SOM
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Collections.Specialized.NameValueCollection nv = HttpUtility.ParseQueryString(Request.Url.Query, System.Text.Encoding.Default);
            string CaseType = nv["CaseType"];
            //edit by hujianhua 20100712 for issue 1430
             string FilePath= string.Empty;
            if (Session[BLL.COM.Session.COM_DownLoadFilePathTemp] != null)
            {
                FilePath = Session[BLL.COM.Session.COM_DownLoadFilePathTemp].ToString();
            }
            Session[BLL.COM.Session.COM_DownLoadFilePathTemp] = null; 

            if (String.IsNullOrEmpty(FilePath))
            {
                Response.End();
                return;
            }
            //FilePath = APJUtil.Utility.ConvertFromBase64(FilePath); //edit by hujianhua 20100712 for issue 1430
            if (!File.Exists(FilePath))
            {
                Response.End();
                return;
            }
            string Extention = Path.GetExtension(FilePath).ToLower();

            string strImageExtension = "|.jpg|.bmp|.gif|.gpeg|.png";
            if (File.Exists(FilePath))
            {
                if (!strImageExtension.Contains("|" + Extention))//(Extention == ".pdf" || Extention == ".doc")
                {
                    FileInfo fi = new FileInfo(FilePath);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.HeaderEncoding = System.Text.Encoding.Default;
                    Response.Buffer = false;
                    Response.ContentType = APJ.Util.Utility.GetContentType(Extention);//"application/octet-stream";//MiniDict[Extention];
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + fi.Name);
                    Response.AppendHeader("Content-Length", fi.Length.ToString());
                    Response.WriteFile(fi.FullName);
                    Response.Flush();
                    Response.Close();
                }
                else
                {
                    System.Drawing.Imaging.ImageFormat SelectedFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    switch (Extention)
                    {
                        case ".png":
                            SelectedFormat = System.Drawing.Imaging.ImageFormat.Png;
                            break;
                        case ".gif":
                            SelectedFormat = System.Drawing.Imaging.ImageFormat.Gif;
                            break;
                        case ".bmp":
                            SelectedFormat = System.Drawing.Imaging.ImageFormat.Bmp;
                            break;
                        default:
                            SelectedFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                            break;
                    }

                    System.Drawing.Bitmap SelectedBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(FilePath);
                    //update by wpd 20080818 for bmp png do not show 
                    SelectedBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    SelectedBitmap.Dispose();
                    Response.End();
                    Response.Close();
                }
            }
            else
            {
                Response.Write("<center><div class=\"required\">The file doesn't exist.</div></center>");
            }
        }
    }
}
